<?php

namespace Act;

use core\controller\baseController;
use Act\Logic\ActLogic;
use \common\Factory\Factory;

class ActController extends baseController {
    
    function __construct() {
        $redis = Factory::getServer('Redis');
        $redis && $redis->hIncrBy('ad_act_pv', date('Ymd', time()), 1);
    }
    
    public function fromTd() {
        Factory::getServer('MqWriteLog')->connect();
        $actLogic = new ActLogic();
        if(isset($_GET['ADNETNAME'])) {
            $config = \core\init\init::getConfig();
            $adTypeToWenZi = $config['adTypeToWenZi'];
            if(isset($adTypeToWenZi[$_GET['ADNETNAME']])) {
                $callback = ucfirst($adTypeToWenZi[$_GET['ADNETNAME']]);
                $observer = "Act\\Logic\\Event\\{$callback}Callback";
                class_exists($observer) && $actLogic->addObserver(new $observer());
            }
        }
        $actLogic->addObserver(new \Act\Logic\Event\TdSelectIdfaFromFddIosCallback());
        $actLogic->addObserver(new \Act\Logic\Event\RedisCallback());
        
        Factory::getServer('MqWriteLog')->send('act insert raw request params:' . json_encode($_GET));
        $data = false;
        if(count($_GET) > 0) {
            isset($_GET['APPSTOREID']) && empty($_GET['APPSTOREID']) && $_GET['APPSTOREID'] = $this->routeParams['appId'];
            !isset($_GET['APPSTOREID']) && $_GET['APPSTOREID'] = $this->routeParams['appId'];
            $data = $actLogic->insert($_GET);
        }
        Factory::getServer('MqWriteLog')->disConnect();
        
        $this->jsonReturn($data);
    }
    
    public function fromChannel() {
        $data = false;
        if (empty($_GET['adid']) $this->jsonReturn($data);
        
        Factory::getServer('MqWriteLog')->connect();
        $config = \core\init\init::getConfig();
        $aid = $_GET['adid'];
        $channel = array_search($aid, $config['adType']);
        $data = false;
        
        if($channel !== false) {
            $channel = ucfirst($channel);
            $logicName = "Act\\Logic\\Channel\\{$channel}Channel";
            if(class_exists($logicName)) {
                $channelLogic  = new $logicName();
                $data = $channelLogic->act($_GET, $aid, $channel);
            }
        } 
        Factory::getServer('MqWriteLog')->disConnect();
        
        $this->jsonReturn($data);
    }
}